<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Image extends CIC_A_Controller
{
	var $category;
	function __construct()
	{
		parent::__construct();
		$this->init();
	}	
	
	function init()
	{
		$this->category = $this->cic_model->get_all('image_category');
		$this->load->library('image_lib');
	}
	function category()
	{
		
	}
	
	function box_show()
	{
		$data['CKEditorFuncNum'] = $this->input->get('CKEditorFuncNum');
		$data['category'] = $this->category;
		$sys_config = $this->get_config('ciccms/admin','sys');
		$data['thumb_width']  	= $sys_config['thumb_width'];
		$data['thumb_height'] 	= $sys_config['thumb_height'];
		$data['image_type']		= $sys_config['image_type'];
		$data['type']			= $this->uri->segment(4);
		$this->load->view('admin/include/image_box',$data);
	}
	function get_image()
	{
		$query = $this->cic_model->get_all('image',array('category_id'=>$this->input->post('category_id')),array('image_upload_time','desc'));
		if($query)
		{
			foreach ($query->result() as $key=>$value)
			{
				$data[$key]['image_id'] = $value->image_id;
				$data[$key]['category_id'] = $value->category_id;
				$data[$key]['image_path'] = $value->image_path;
				$data[$key]['image_alt'] = $value->image_alt;
				$data[$key]['image_thumb_path'] = $value->image_thumb_path;
			}
			echo json_encode($data);
		}else{
			echo 0;
		}
	}
	
	function upload()
	{
		$this->do_upload();
	}
	
	private function do_upload()
	{
		$image_config 	= $this->get_config('ciccms/admin','sys');
		$category_id 	= $this->input->post('pic_cate');
		$thumb			= $this->input->post('thumb');
		$thumb_width	= $this->input->post('thumb_width');
		$thumb_height	= $this->input->post('thumb_height');
		$date = date('Y-m-d');
		$upload_path = 'upload/image/';
		if(! file_exists(realpath($upload_path).'/'.$date.'/'))
			mkdir(realpath($upload_path).'/'.$date.'/',0777,true);
		$path = $upload_path.$date.'/';
		$counts = count($_FILES);
		$config = array(
				'upload_path' 	=> $path,
				'allowed_types' => $image_config['image_type'],
				'encrypt_name'	=> true,
		);
		$this->load->library('upload',$config);
		for($i=1;$i <= $counts;$i++)
		{
			if(! $this->upload->do_upload('image'.$i))
				$this->message->msg($this->upload->display_errors(),site_url('admin/image/box_show'));

			$data = $this->upload->data();
			if($this->get_config('ciccms/admin','sys','water_status') == '1')
			{
				$this->water($data['full_path']);
			}
			
			if($thumb)
			{
				$this->thumb($data['full_path'],$thumb_width,$thumb_height);
				$this->cic_model->insert('image',array('category_id'=>$category_id,'image_path'=>$path.$data['file_name'],'image_thumb_path'=>$path.$data['raw_name'].'_thumb'.$data['file_ext'],'image_alt'=>$this->input->post('image_alt'.$i),'image_upload_time'=>time()));
			}else{
				$this->cic_model->insert('image',array('category_id'=>$category_id,'image_path'=>$path.$data['file_name'],'image_alt'=>$this->input->post('image_alt'.$i),'image_upload_time'=>time()));
			}
			
			
		}
		$this->message->msg('上传成功！',site_url('admin/image/box_show'));
	}
	
	private function water($source_image)
	{
		
		$config['source_image'] = $source_image;
		$position				= explode('_',$this->get_config('ciccms/admin','sys','water_position'));
		$config['wm_vrt_alignment'] = $position[0];
		$config['wm_hor_alignment'] = $position[1];
		if($this->get_config('ciccms/admin','sys','water_type') == 'image')
		{
			$config['wm_type']			 = 'overlay';
			$config['wm_overlay_path']	 = $this->get_config('ciccms/admin','sys','water_path');			
		}else{
			$config['wm_type'] = 'text';
			$config['wm_text'] = $this->get_config('ciccms/admin','sys','water_word');
			$config['wm_font_path'] = './system/fonts/texb.ttf';
			$config['wm_font_size'] = $this->get_config('ciccms/admin','sys','water_word_size');
			$config['wm_font_color'] = $this->get_config('ciccms/admin','sys','water_word_color');
		}
		$this->image_lib->initialize($config); 
		$this->image_lib->watermark();
	}

	private function thumb($source_image,$width,$height)
	{
		$pathinfo = pathinfo($source_image);
		$new_image = $pathinfo['filename'].'_thumb.'.$pathinfo['extension'];
		$config = array(
				'image_library' => 'gd2',
				'source_image'	=> 	$source_image,
				'create_thumb'	=> TRUE,
				'maintain_ratio'=> TRUE,
				'width'			=> $width,
				'height'		=> $height,
				'new_image'		=> $new_image,
		);
		$this->image_lib->initialize($config);
		if(! $this->image_lib->resize())
		{
			$this->message->msg($this->image_lib->display_errors(),site_url('admin/image/box_show'));
		}
	}

}